package com.chainyoung.component_tmtrade.ui.mvp.presenter;

import android.text.TextUtils;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.di.scope.FragmentScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.bean.FiatTradeListItem;
import com.chainyoung.component_tmtrade.bean.TradeResult;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeFiatCurrencyBuyContract;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeFiatCurrencySubmitOrderContract;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;


@ActivityScope
public class TradeFiatCurrencySubmitOrderPresenter extends BasePresenter<TradeFiatCurrencySubmitOrderContract.Model, TradeFiatCurrencySubmitOrderContract.View> {
    @Inject
    public TradeFiatCurrencySubmitOrderPresenter(TradeFiatCurrencySubmitOrderContract.Model model, TradeFiatCurrencySubmitOrderContract.View rootView) {
        super(model, rootView);
    }


    public void checkSubmit(int type, String id, String price, String num, List<String> bankIds, String infoMsg, String coinName) {
        if (TextUtils.isEmpty(num)) {
            mView.showMsg(R.string.tmtrade_pls_input_num);
            return;
        }
        if (type == 2 && bankIds.isEmpty()) {
            mView.showMsg(R.string.tmtrade_pls_set_recieve_addr);
            return;
        }
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("type", type);
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("id", id);
        map.put("num", num);
        if (type == 2) {
            map.put("bank_ids", StringUtil.listToString(bankIds));
        }
        map.put("market", coinName.toLowerCase() + "_aud");
        submitOrder(map, infoMsg);
    }


    public void submitOrder(TreeMap<String, Object> map, String infoMsg) {
        addSubscribe(mModel.submitFiatCurrencyOrder(map)
                .compose(RxUtils.<TradeResult>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<TradeResult>(mView, true, infoMsg) {
                    @Override
                    public void onNext(TradeResult data) {
                        mView.onSubmitOrderSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


    public void getRecieveAccount(TreeMap<String, Object> map) {
        addSubscribe(mModel.getRecieveAccount(map)
                .compose(RxUtils.<List<RecieveAccount>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<RecieveAccount>>(mView) {
                    @Override
                    public void onNext(List<RecieveAccount> data) {
                        mView.onGetRecieveAccount(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetRecieveAccount(new ArrayList<RecieveAccount>());
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }


}
